package du;
import robocode.*;
import robocode.util.Utils;
import java.awt.Color;


public class Vincent extends AdvancedRobot {
	static int ahead = 1; // what direction i am going
	static final double menKan = 200; // specify your own value
	static double enemyEnergy;	//Energy of anemy rob.
	private static double v;	//Bullet speed.

	public void run() {
		//Set colors
		setColors(Color.lightGray,Color.pink,Color.yellow, Color.red, Color.cyan);
		//Infinity lock
		turnRadarRightRadians(Double.POSITIVE_INFINITY); 
	}

	public void onScannedRobot(ScannedRobotEvent e) {
		//Change direction.
		if(e.getEnergy()<enemyEnergy && e.getEnergy()>enemyEnergy-3.5) {
			//If ememy fired.
			ahead *= -1;
		}
		//Infinity lock
		setTurnRadarLeftRadians(getRadarTurnRemainingRadians()); 
		//Noniterative Linear Targeting
		double absBear = getHeadingRadians() + e.getBearingRadians();
		double eDistance = e.getDistance();	//Distance to the enemy rob
		//Shoot different bullet based on the enemy distance.
		double bulletPower = Math.min(Math.min(3, 3*menKan/eDistance), getEnergy());
		v = (8 * v + e.getVelocity()) / 9;	//Consider the previous speed.
		double gunTurnAngle = absBear - getGunHeadingRadians();
		//A bullet's speed is calculated with a formula based on its bullet power
		if (bulletPower<3 || Math.random()>0.2) {
			gunTurnAngle += v/(20-(3*bulletPower))*Math.sin(e.getHeadingRadians()-absBear);
		}
		//Gun turn based on linear targeting
		setTurnGunRightRadians(Utils.normalRelativeAngle(gunTurnAngle));
		//Fire smartly.		
		setFire(bulletPower);			
		//Move based on direction and distance.
		setAhead(menKan*ahead*Math.abs(ahead-2)*Math.max(1,eDistance/menKan)); 
		//Stay Perpendicular
		setTurnLeftRadians(Math.cos(e.getBearingRadians()) * Math.PI/2); 
		//For next time.
		enemyEnergy = e.getEnergy();		
	}

	public void onHitWall(HitWallEvent e) {ahead *= -1;}	
}
